package com.gitee.wsl.common.draw.zdog.render

import com.gitee.wsl.ext.base.ifValue
import kotlin.math.abs
import kotlin.math.sqrt

const val TAU = Math.PI * 2
const val arcHandleLength = 9 / 16f


fun lerp(a: Float, b: Float, alpha: Float): Float {
    return (b - a) * alpha + a
}

fun modulo(num: Float, div: Float): Float {
    return ((num % div) + div) % div
}

fun magnitudeSqrt(sum: Float): Float {
    if (abs(sum - 1) < 0.00000001) {
        return 1f
    }
    return sqrt(sum)
}

private val powerMultipliers = arrayOf(
    { a: Float -> a * a },
    { a: Float -> a * a * a },
    { a: Float -> a * a * a * a },
    { a: Float -> a * a * a * a * a }
)

fun powerMultipliers(power: Int) = powerMultipliers[power - 2]

fun easeInOut(alpha1: Float, power: Int = 2): Float {
    if (power == 1) return alpha1
    val alpha = 0f.coerceAtLeast(1f.coerceAtMost(alpha1))
    val isFirstHalf = alpha < 0.5f
    var slope =  (isFirstHalf).ifValue( alpha , 1 - alpha)
    slope /= 0.5f
    val powerMultiplier = (power < 2 || power > 5).ifValue( powerMultipliers(2),
        powerMultipliers(power)
    )
    var curve = powerMultiplier(slope)
    curve /= 2f
    return if (isFirstHalf) curve else 1 - curve
}
